import { getPageList,getAddUser,getDelete,getAllDeleteData,UPdata,assignUserRoleListApi,getUserId} from "@/api/controller ";
const state = {
  pageList: {},
  allroleslist:[],//全部的角色Id
  assignRoles:[]//选中的角色Id
};

const mutations = {
  //获取分页列表
  SET_PAGELIST(state, pageList) {
    state.pageList = pageList;
  },
  SET_ALLROLESLIST(state,allroleslist){
    state.allroleslist = allroleslist
  },
  SET_ASSIGNROLES(state,assignRoles){
    state.assignRoles=assignRoles
  }
};

const actions = {
  //获取分页列表
  async getPageList({ commit },pageLimit) {
    try {
     const result = await getPageList(pageLimit.page,pageLimit.limit,{username:pageLimit.username});
      commit("SET_PAGELIST", result);
      return "ok";
    } catch (error) {
      return Promise.reject(new Error("请求获取分页列表失败"));
    }
  },
  //点击添加用户
 async getAddUser({commit},userForm){
  console.log(userForm);
  try {
    await getAddUser(userForm)
    return 'ok'
  } catch (error) {
    return Promise.reject(new Error('请求点击添加用户失败'))
  }
  },
  //点击修改
  async undata({commit},userForm){
    try {
      const result = await UPdata(userForm)
      console.log('result',result);
    } catch (error) {
    return Promise.reject(new Error('请求修改用户失败'))
    }
  },
  //删除单个
  async getDeleteOneData({commit},id){
    try {
      await getDelete(id.id)
    } catch (error) {
    return Promise.reject(new Error('请求点击删除用户失败'))
    }
  },
  // 点击删除多个
  async getAllDeleteData({commit},ids){
    try {
      await getAllDeleteData(ids)
    } catch (error) {
    return Promise.reject(new Error('请求点击删除多个用户失败'))
    }
  },
  //获取用户id
  async getId({commit},userId){
    console.log('2222@',userId);
    
    try {
      const result = await getUserId(userId)
      commit('SET_ALLROLESLIST','SET_ASSIGNROLES',result)
      console.log('result--->',result);
    } catch (error) {
    return Promise.reject(new Error('请求获取用户Id失败'))
    }
  },
  //分配用户
async assignUser({commit},userId,roleIds){
  console.log('2222111-->',userId,roleIds);
  try {
    const result = await assignUserRoleListApi(userId,roleIds)
    console.log('@@@--->',res);
    
  } catch (error) {
    
  }
}
};

const getters = {
    itemsList(state){
        return (state.pageList.data||{}).items||[]
    },
    dataTodal(state){
        return state.pageList.data||{}
    },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
};
